package com.ruibang.glass.produce.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.common.constants.RedisKey;
import com.ruibang.glass.produce.constant.Constants;
import com.ruibang.glass.produce.domain.req.CommonTrendChartReq;
import com.ruibang.glass.produce.domain.req.GlassViscosityReq;
import com.ruibang.glass.produce.domain.resp.GlassViscosityTrendCharInfo;
import com.ruibang.glass.produce.entity.GlassViscosity;
import com.ruibang.glass.produce.mapper.GlassViscosityMapper;
import com.ruibang.glass.produce.service.GlassViscosityService;
import com.ruibang.glass.produce.util.ExcelStyleUtils;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 生产-理化-玻璃粘度 服务实现类
 * </p>
 *
 * @author songJinKang
 * @since 2024-03-12
 */
@Slf4j
@Service
public class GlassViscosityServiceImpl extends ServiceImpl<GlassViscosityMapper, GlassViscosity> implements GlassViscosityService {


    @Autowired
    private RedisSingleService redisService;

    @Override
    public IPage<GlassViscosity> pageQuery(GlassViscosityReq glassViscosityReq) {
        QueryWrapper<GlassViscosity> qw = new QueryWrapper<>();
        if (glassViscosityReq.getStartTime() != null && glassViscosityReq.getEndTime() != null) {
            qw.lambda().ge(GlassViscosity::getTime, glassViscosityReq.getStartTime());
            qw.lambda().le(GlassViscosity::getTime, glassViscosityReq.getEndTime());
        }
        qw.lambda().orderByDesc(GlassViscosity::getTime);
        return this.page(new Page<>(glassViscosityReq.getPageNo(), glassViscosityReq.getPageSize()), qw.lambda());
    }

    @Override
    public void delete(String id) {
        lambdaUpdate().eq(GlassViscosity::getId, id).set(GlassViscosity::getIsDelete, Constants.ONE_STR).update();
    }

    @Override
    public GlassViscosityTrendCharInfo trendChart(CommonTrendChartReq trendChartReq) {
        //默认查询3天
        if (trendChartReq.getStartTime() == null || trendChartReq.getEndTime() == null) {
            LocalDateTime now = LocalDateTime.now();
            trendChartReq.setStartTime(now.minusDays(3));
            trendChartReq.setEndTime(now);
        }
        //组合数据
        GlassViscosityTrendCharInfo glassViscosityTrendCharInfo = new GlassViscosityTrendCharInfo();
        List<GlassViscosity> data = baseMapper.trendChart(trendChartReq);
        if (CollectionUtils.isNotEmpty(data)) {
            glassViscosityTrendCharInfo.setCollectTime(data.stream().map(e -> e.getTime().format(Constants.DATE_TIME_FORMATTER)).collect(Collectors.toList()));
            glassViscosityTrendCharInfo.setData(data);
            //玻璃粘度1
            glassViscosityTrendCharInfo.setF2(data.stream().map(GlassViscosity::getF2).collect(Collectors.toList()));
            glassViscosityTrendCharInfo.setF3(data.stream().map(GlassViscosity::getF3).collect(Collectors.toList()));
            glassViscosityTrendCharInfo.setF4(data.stream().map(GlassViscosity::getF4).collect(Collectors.toList()));
            glassViscosityTrendCharInfo.setF5(data.stream().map(GlassViscosity::getF5).collect(Collectors.toList()));
            glassViscosityTrendCharInfo.setF6(data.stream().map(GlassViscosity::getF6).collect(Collectors.toList()));
            glassViscosityTrendCharInfo.setF7(data.stream().map(GlassViscosity::getF7).collect(Collectors.toList()));
            glassViscosityTrendCharInfo.setF8(data.stream().map(GlassViscosity::getF8).collect(Collectors.toList()));

            //玻璃粘度2
            glassViscosityTrendCharInfo.setF1(data.stream().map(GlassViscosity::getF1).collect(Collectors.toList()));
        }
        return glassViscosityTrendCharInfo;
    }

    @Override
    public void export(GlassViscosityReq glassViscosityReq, HttpServletResponse response) {
        glassViscosityReq.setPageNo(-1);
        glassViscosityReq.setPageSize(-1);
        IPage<GlassViscosity> page = this.pageQuery(glassViscosityReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            List<GlassViscosity> glassViscosityList = page.getRecords();
            try {
                EasyExcel.write(response.getOutputStream(), GlassViscosity.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("history_data").doWrite(glassViscosityList);
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }

    @Override
    public void saveInfo(GlassViscosity glassViscosity) {
        setUserName(glassViscosity);
        this.save(glassViscosity);
    }

    @Override
    public void edit(GlassViscosity glassViscosity) {
        setUserName(glassViscosity);
        this.updateById(glassViscosity);
    }

    private void setUserName(GlassViscosity glassViscosity) {
        glassViscosity.setIsDelete(Constants.ZERO_STR);
        String checkById = glassViscosity.getCheckById();
        String createUserId = glassViscosity.getCreateUserId();
        if (StringUtils.isNotBlank(checkById)) {
            Object userObj = redisService.hget(RedisKey.USER_GROUP_TYPE, checkById);
            if (userObj != null && StringUtils.isNotBlank(userObj.toString())) {
                glassViscosity.setCheckBy(userObj.toString());
            }
        } else {
            glassViscosity.setCheckBy("");
        }
        if (StringUtils.isNotBlank(createUserId)) {
            Object userObj = redisService.hget(RedisKey.USER_GROUP_TYPE, createUserId);
            if (userObj != null && StringUtils.isNotBlank(userObj.toString())) {
                glassViscosity.setCreateUserName(userObj.toString());
            }
        } else {
            glassViscosity.setCreateUserName("");
        }
    }
}
